package com.hcl.security.handle;

import java.io.IOException;
import java.io.PrintWriter;

import com.hcl.security.utils.JsonUtils;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.authentication.AuthenticationFailureHandler;

import com.hcl.security.utils.Result;

import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;

/**
 * 登录失败处理器
 */
@Slf4j
public class LoginAuthenticationFailureHandler
		implements AuthenticationFailureHandler {
	@Override
	public void onAuthenticationFailure(HttpServletRequest request,
			HttpServletResponse response, AuthenticationException exception)
			throws IOException {
		response.setContentType("application/json;charset=UTF-8");
		response.setStatus(HttpServletResponse.SC_UNAUTHORIZED);
		PrintWriter writer = response.getWriter();
		writer.println(JsonUtils.objectToJson(Result.error(
				HttpServletResponse.SC_UNAUTHORIZED, exception.getMessage())));
	}
}
